package com.wxb.controller;

import com.github.pagehelper.PageHelper;
import com.wxb.common.core.utils.DateUtils;
import com.wxb.common.core.web.domain.AjaxResult;
import com.wxb.common.security.utils.SecurityUtils;
import com.wxb.domain.MallFootprintEntity;
import com.wxb.service.IMallFootprintService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Date 2025-05-30 15:29
 * @Version 1.0
 * @Description
 * @ClassName MallFootprintController
 **/
@RestController
@RequestMapping("/footprint")
public class MallFootprintController {
    @Autowired
    private IMallFootprintService footprintService;

    /**
     */
    @ApiOperation(value = "删除足迹")
    @ApiImplicitParams({@ApiImplicitParam(name = "footprintId", value = "足迹id", paramType = "path", required = true)})
    @PostMapping("delete")
    public Object delete(Integer footprintId) {
        Long loginUserId =SecurityUtils.getUserId();
        if (footprintId == null) {
            return AjaxResult.error("删除出错");
        }
        //删除当天的同一个商品的足迹
        MallFootprintEntity footprintEntity = footprintService.queryObject(footprintId);
        //
        if (loginUserId == null || loginUserId == null || footprintEntity == null || footprintEntity.getGoodsId() == null) {
            return AjaxResult.error("删除出错");
        }

        Map<String, Object> param = new HashMap<String, Object>();
        param.put("userId", loginUserId);
        param.put("goodsId", footprintEntity.getGoodsId());
        footprintService.deleteByParam(param);

        return AjaxResult.success("删除成功");
    }

    /**
     */
    @ApiOperation(value = "获取足迹列表")
    @PostMapping("list")
    public Object list(
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Map<String, Object> resultObj = new HashMap<String, Object>();

        //查询列表数据
        PageHelper.startPage(0, 10, false);
        List<MallFootprintEntity> footprintVos = footprintService.queryListFootprint(SecurityUtils.getUserId() + "");

        //
        Map<String, List<MallFootprintEntity>> footPrintMap = new TreeMap<String, List<MallFootprintEntity>>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {

                //指定排序器按照降序排列
                return o2.compareTo(o1);
            }
        });

        if (null != footprintVos && footprintVos.size() > 0) {
            for (MallFootprintEntity footprintVo : footprintVos) {
                String addTime = DateUtils.timeToStr(footprintVo.getAddTime(), DateUtils.YYYY_MM_DD);
                List<MallFootprintEntity> tmpList = footPrintMap.get(addTime);
                if (null == footPrintMap.get(addTime)) {
                    tmpList = new ArrayList<MallFootprintEntity>();
                }
                tmpList.add(footprintVo);
                footPrintMap.put(addTime, tmpList);
            }
            List<List<MallFootprintEntity>> footprintVoList = new ArrayList<List<MallFootprintEntity>>();
            for (Map.Entry<String, List<MallFootprintEntity>> entry : footPrintMap.entrySet()) {
                footprintVoList.add(entry.getValue());
            }
        }

        return AjaxResult.success(footprintVos);
    }


    /**
     */
    @ApiOperation(value = "分享足迹")
    @PostMapping("sharelist")
    public Object sharelist(
                            @RequestParam(value = "page", defaultValue = "1") Integer page,
                            @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Map<String, List<MallFootprintEntity>> resultObj = new HashMap<String, List<MallFootprintEntity>>();

        //查询列表数据
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sidx", "f.id");
        params.put("order", "desc");
        params.put("referrer", SecurityUtils.getUserId());
        List<MallFootprintEntity> footprintVos = footprintService.shareList(params);
        //
        resultObj.put("data", footprintVos);
        return AjaxResult.success(resultObj);
    }
}
